package com.example.emos.api.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.db.dao.TbMeetingDao;
import com.example.emos.api.db.pojo.TbMeeting;
import com.example.emos.api.exception.EmosException;
import com.example.emos.api.service.MeetingService;
import com.example.emos.api.task.MeetingWorkflowTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author keying
 * @create 2024--05--19
 */
@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private TbMeetingDao meetingDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MeetingWorkflowTask meetingWorkflowTask;
    @Override
    public PageUtils searchOfflineMeetingByPage(HashMap param) {
        ArrayList<HashMap> list = meetingDao.searchOfflineMeetingByPage(param);
        long count = meetingDao.searchOfflineMeetingCount(param);
        int start = (int) param.get("start");
        int length = (int) param.get("length");

        for (HashMap map : list){
            String meeting = (String) map.get("meeting");

            if (meeting != null && meeting.length() > 0){
                map.replace("meeting", JSONUtil.parseArray(meeting));
            }
        }

        PageUtils pageUtils = new PageUtils(list, count, start, length);
        return pageUtils;

    }

    @Override
    public int insert(TbMeeting meeting) {
        int rows = meetingDao.insert(meeting);
        if (rows != 1){
            throw new EmosException("会议添加失败");
        }
        Short type = meeting.getType();
        meetingWorkflowTask.startMeetingWorkflow(meeting.getUuid(),meeting.getCreatorId(),meeting.getTitle(),meeting.getDate(),
                meeting.getStart() + ":00",type == 1 ? "线上会议" : "线下会议");
        return rows;
    }

    @Override
    public ArrayList<HashMap> searchOfflineMeetingInWeek(HashMap param) {
        ArrayList<HashMap> list = meetingDao.searchOfflineMeetingInWeek(param);
        return list;
    }

    @Override
    public HashMap searchMeetingInfo(short status, long id) {
        HashMap map;
        if (status == 4 || status == 5){
            map = meetingDao.searchCurrentMeetingInfo(id);
        }else{
            map = meetingDao.searchMeetingInfo(id);
        }
        return map;
    }

    @Override
    public int deleteMeetingApplication(HashMap param) {
        Long id = MapUtil.getLong(param, "id");
        String uuid = MapUtil.getStr(param, "uuid");
        String instanceId = MapUtil.getStr(param, "instanceId");
        //查询会议，不足20分钟，不能删除
        HashMap meeting = meetingDao.searchMeetingById(param);
        String date = MapUtil.getStr(meeting, "date");
        String start = MapUtil.getStr(meeting, "start");
        int status = MapUtil.getInt(meeting,"status");
        boolean isCreator = Boolean.parseBoolean(MapUtil.getStr(meeting, "isCreator"));
        DateTime dateTime = DateUtil.parse(date + " " + start);
        DateTime now = DateUtil.date();
        if (now.isAfterOrEquals( dateTime.offset(DateField.MINUTE,-20))) {
            throw  new EmosException("距离会议开始不足20分钟，不能删除会议");
        }

        //只能申请人删除会议
        if (!isCreator){
            throw new EmosException("只能申请人删除该会议");
        }
        //status == 1 || status == 3 才能删除
        if (status == 1 || status == 3){
            int rows = meetingDao.deleteMeetingApplication(param);
            if (rows == 1){
                String reason = MapUtil.getStr(param, "reason");
                meetingWorkflowTask.deleteMeetingApplication(uuid,instanceId,reason);
            }
            return rows;
        }else{
            throw new EmosException("只能删除待审批和未开始的会议");
        }
    }

    @Override
    public PageUtils searchOnlineMeetingByPage(HashMap param) {
        ArrayList<HashMap> list = meetingDao.searchOnlineMeetingByPage(param);
        long count = meetingDao.searchOnlineMeetingCount(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        return new PageUtils(list,count,start,length);
    }

    @Override
    public Long searchRoomIdByUUID(String uuid) {
        if (redisTemplate.hasKey(uuid)){
            Object temp = redisTemplate.opsForValue().get(uuid);
            long roomId = Long.parseLong(temp.toString());
            return roomId;
        }
        return null;
    }

    @Override
    public ArrayList<HashMap> searchOnlineMeetingMembers(HashMap param) {
        ArrayList<HashMap> list = meetingDao.searchOnlineMeetingMembers(param);
        return list;
    }
}
